package com.bc004346.travelus.DataAccess.AsyncTasks;

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;

import com.bc004346.travelus.DataAccess.TravelUsScreenScraper;
import com.bc004346.travelus.Model.Domain.Passenger;
import com.bc004346.travelus.Model.Domain.PassengerList;
import com.bc004346.travelus.Model.Interface.IGetPriceCallback;
import com.bc004346.travelus.Utility.Utility;

import java.util.ArrayList;
import java.util.List;

import static com.bc004346.travelus.Utility.Utility.log;

/**
 * Created by Renats on 4/1/14.
 * Get Travel Price
 */
public class GetPriceTaskFragment extends Fragment {

    //<editor-fold desc="Members">
    private static final String PASSENGERS = "passengers";
    private IGetPriceCallback mCallbacks;
    private TravelUsScreenScraper mTravelUS;
    private PassengerList mSelectedPassengers;
    //</editor-fold>

    //<editor-fold desc="Static Methods">
    public static GetPriceTaskFragment newInstance(PassengerList passengers) {
        GetPriceTaskFragment f = new GetPriceTaskFragment();
        Bundle args = new Bundle();
        args.putSerializable(PASSENGERS, passengers);
        f.setArguments(args);
        return f;
    }
    //</editor-fold>

    //<editor-fold desc="Overrides">
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallbacks = (IGetPriceCallback) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        mTravelUS = Utility.getInstance().getTravelUS();
        mSelectedPassengers = (PassengerList) (getArguments() == null ? null : getArguments().getSerializable(PASSENGERS));
        new GetPriceTask().execute();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }
    //</editor-fold>

    private class GetPriceTask extends AsyncTask<Void, Void, Float> {

        private Exception ex = null;

        @Override
        protected void onPostExecute(Float param) {
            if (mCallbacks != null) {
                mCallbacks.onGetPricePostExecute(param, ex);
            }
        }

        @Override
        protected Float doInBackground(Void... params) {
            float price = (long) -1;
            try {
                Passenger pax = mSelectedPassengers.getPassengers().get(mSelectedPassengers.getPassengers().size() - 1);
                List<String> list = new ArrayList<String>();
                for (Passenger pass : mSelectedPassengers.getPassengers())
                    list.add(pass.getLogin());
                price = mTravelUS.getPrice(pax, list);
            } catch (Exception e) {
                log(String.format("Exception: %s", e.toString()));
                ex = e;
            }
            return price;
        }
    }
}
